package com.mud.mudcommune.http;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.annotation.NonNull;

import com.mud.mudcommune.app.App;
import com.mud.mudcommune.utils.LogUtils;

import java.io.File;
import java.util.concurrent.TimeUnit;

import okhttp3.Cache;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;


/**
 * Creator 创建者  by Administrator
 * 描述:网络下载库
 */
public class NetUtils {

    public static final int NET_OK = 1;
    public static final int NET_WEAK = 2;
    public static final int NET_STOP = 3;
    public static final String ACTION_NETSTATE = "com.xm.trader.v3.receiver.NetStateBroadcast.action.netstate";

    private static OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient.Builder();

    private static File httpCacheDirectory = new File(App.mContext.getCacheDir(), "responses");
    private static int cacheSize = 10 * 1024 * 1024; // //设置缓存 10MiB
    private static Cache cache = new Cache(httpCacheDirectory, cacheSize);

    private static Retrofit.Builder builder =
            new Retrofit.Builder()
                    .addConverterFactory(GsonConverterFactory.create())//增加返回值为String的支持
                    //.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                    .addConverterFactory(ScalarsConverterFactory.create());//增加返回值为Gson的支持(以实体类返回)

    private static HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
        @Override
        public void log(String message) {
            //打印retrofit日志
            LogUtils.w("RetrofitLog", "RetrofitBack = " + message);
        }
    }).setLevel(HttpLoggingInterceptor.Level.BODY);

    public static <S> S createService(Class<S> serviceClass, @NonNull String baseURL) {



        OkHttpClient okHttpClient = okHttpClientBuilder
                .cache(cache)
                .connectTimeout(10, TimeUnit.SECONDS)//连接超时时间
                .addNetworkInterceptor(httpLoggingInterceptor)
                .writeTimeout(10, TimeUnit.SECONDS)
                .readTimeout(30, TimeUnit.SECONDS)
                .build();
        Retrofit retrofit = builder.baseUrl(baseURL).client(okHttpClient)
                .build();

        return retrofit.create(serviceClass);
    }

    /**
     * 没有连接网络
     */
    private static final int NETWORK_NONE = -1;
    /**
     * 移动网络
     */
    private static final int NETWORK_MOBILE = 0;
    /**
     * 无线网络
     */
    private static final int NETWORK_WIFI = 1;

    public static int getNetWorkState(Context context) {
        // 得到连接管理器对象
        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo activeNetworkInfo = connectivityManager
                .getActiveNetworkInfo();
        if (activeNetworkInfo != null && activeNetworkInfo.isConnected()) {
            if (activeNetworkInfo.getType() == (ConnectivityManager.TYPE_WIFI)) {
                return NETWORK_WIFI;
            } else if (activeNetworkInfo.getType() == (ConnectivityManager.TYPE_MOBILE)) {
                return NETWORK_MOBILE;
            }
        } else {
            return NETWORK_NONE;
        }
        return NETWORK_NONE;
    }
}


